﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace GMS.BIZ
{
    public class Defense
    {
        /// <summary>
        /// 获取答辩信息
        /// </summary>
        /// <param name="id">流水号</param>
        /// <returns></returns>
        static public DefenseInfo GetDefenseInfo(decimal id)
        {
            GMS.ORM.Model.PDefenseLib myPDfenseLib=new GMS.ORM.Model.PDefenseLib();
            GMS.ORM.DAL.PDefenseLib optPDfenseLib=new GMS.ORM.DAL.PDefenseLib();

            if (!optPDfenseLib.Exists(id))
                return null;

            myPDfenseLib = optPDfenseLib.GetModel(id);

            DefenseInfo myDefenseInfo = new DefenseInfo();
            myDefenseInfo.Id=myPDfenseLib.def_id;
            myDefenseInfo.DefenseTime=myPDfenseLib.def_time;
            myDefenseInfo.DefenseRoom=myPDfenseLib.def_room;
            myDefenseInfo.DefenseTeacher1=myPDfenseLib.def_tch_1;
            myDefenseInfo.DefenseTeacher2 = myPDfenseLib.def_tch_2;
            myDefenseInfo.DefenseTeacher3 = myPDfenseLib.def_tch_3;
            myDefenseInfo.DefenseTeacher4 = myPDfenseLib.def_tch_4;
            myDefenseInfo.DefenseTeacher5 = myPDfenseLib.def_tch_5;
            myDefenseInfo.DefenseAssistant = myPDfenseLib.def_assistant;
            return myDefenseInfo;
        }

        /// <summary>
        /// 获得答辩列表。若不存在，返回null。Dataset:def_id,def_time,def_room,def_tch_1~def_tch_5,def_assistant
        /// </summary>
        /// <param name="studentID">学生学号或教师工号</param>
        /// <param name="year">毕业年份</param>
        static public DataSet GetDefenseList(string ID, string year)
        {
            //判断ID为学号或工号
            //ID为学号
            if (ID.Length == 9)
            {
                GMS.ORM.DAL.PDefense optPDfense = new GMS.ORM.DAL.PDefense();
                GMS.ORM.Model.PDefenseLib myPDfenseLib = new GMS.ORM.Model.PDefenseLib();
                GMS.ORM.DAL.PDefenseLib optPDfenseLib = new GMS.ORM.DAL.PDefenseLib();

                try
                {
                    decimal? def_id = (optPDfense.GetModel(ID, year)).pdefense_lib;
                    if (def_id == null)
                        return null;
                    return optPDfenseLib.GetList("def_id=" + def_id);
                }
                catch 
                {
                    return null;
                }
                

                
            }
            //ID为工号
            else if (ID.Length == 5)
            {
                GMS.ORM.DAL.PDefenseLib optPDefenseLib = new GMS.ORM.DAL.PDefenseLib();
                string sqlString = "datepart(year,def_time)=datepart(year,'" + year + "') AND ( 1=2 ";
                for (int i = 1; i < 6; i++)
                {
                    sqlString = sqlString + "OR def_tch_" + i.ToString() + "='" + ID + "' ";
                }
                sqlString = sqlString + ")";
                return optPDefenseLib.GetList(sqlString);
            }
            else
                return null;
        }

        /// <summary>
        /// 获得本届答辩列表。若不存在，返回null。Dataset:def_id,def_time,def_room,def_tch_1~def_tch_5,def_assistant
        /// </summary>
        /// <param name="ID">学生学号或教师工号</param>
        /// <returns></returns>
        static public DataSet GetDefenseList(string ID)
        {
            return GetDefenseList(ID, DeadLine.GetDeadLineYear());
        }

        /// <summary>
        /// 获得某一年所有答辩序号
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        static public ArrayList GetDefenseListByYear(string year)
        {
            ArrayList newArrayList=new ArrayList();

            //测试Year
            if (year.Length != 4) return null;
            int result;
            if (!int.TryParse(year, out result)) return null;

            GMS.ORM.DAL.PDefenseLib optPDefenseLib = new GMS.ORM.DAL.PDefenseLib();
            DataSet ds = optPDefenseLib.GetList("datepart(year,def_time)=datepart(year,'" + year + "') ORDER BY def_time");
            int i = ds.Tables[0].Rows.Count;
            if (i == 0) return null;
            for (int temp = 0; temp < i; temp++)
            {
                newArrayList.Add(decimal.Parse(ds.Tables[0].Rows[temp]["def_id"].ToString()));
            }
            return newArrayList;
        }

        /// <summary>
        /// 获得答辩场次的时间和地点
        /// </summary>
        /// <param name="id">答辩场次序号</param>
        static public string GetDefenseName(decimal id)
        {
            DefenseInfo myDefenseInfo = new DefenseInfo();
            myDefenseInfo = Defense.GetDefenseInfo(id);
            if (myDefenseInfo == null)
                return string.Empty;
            return "时间："+ myDefenseInfo.DefenseTime.ToString() +"  地点："+ myDefenseInfo.DefenseRoom.ToString();
        }

        /// <summary>
        ///获得答辩列表。若不存在，返回null。Dataset:def_id,def_time,def_room,def_tch_1~def_tch_5,def_assistant
        /// </summary>
        /// <param name="studentID">学生学号</param>
        /// <param name="teacherID">教师工号</param>
        /// <returns></returns>
        static public DataSet GetDefenseListST(string studentID, string teacherID)
        {
            GMS.ORM.DAL.PDefense optPDfense = new GMS.ORM.DAL.PDefense();
            GMS.ORM.Model.PDefenseLib myPDfenseLib = new GMS.ORM.Model.PDefenseLib();
            GMS.ORM.DAL.PDefenseLib optPDfenseLib = new GMS.ORM.DAL.PDefenseLib();

            decimal? def_id = optPDfense.GetModel(studentID, DeadLine.GetDeadLineYear()).pdefense_lib;
            
            
            string sqlString = " 1=2 ";
            for (int i = 1; i < 6; i++)
            {
                sqlString = sqlString + "OR def_tch_" + i.ToString() + "='" + teacherID + "' ";
            }
            sqlString = "def_id=" + def_id + " AND ( " + sqlString + " )";
            return optPDfenseLib.GetList(sqlString);
        }
        /// <summary>
        /// 添加一条答辩记录。若成功返回true。
        /// </summary>
        /// <param name="defenseinfo">答辩记录实体对象</param>
        /// <returns></returns>
        public string AddDefenseInfo(DefenseInfo defenseinfo)
        {
            GMS.ORM.Model.PDefenseLib myPDefenseLib = new GMS.ORM.Model.PDefenseLib();
            GMS.ORM.DAL.PDefenseLib optPDefenseLib=new GMS.ORM.DAL.PDefenseLib();
            //myPDefenseLib.def_id = defenseinfo.Id;
            myPDefenseLib.def_time = defenseinfo.DefenseTime;
            myPDefenseLib.def_room = defenseinfo.DefenseRoom;
            myPDefenseLib.def_tch_1 = defenseinfo.DefenseTeacher1;
            myPDefenseLib.def_tch_2 = defenseinfo.DefenseTeacher2;
            myPDefenseLib.def_tch_3 = defenseinfo.DefenseTeacher3;
            myPDefenseLib.def_tch_4 = defenseinfo.DefenseTeacher4;
            myPDefenseLib.def_tch_5 = defenseinfo.DefenseTeacher5;
            myPDefenseLib.def_assistant = defenseinfo.DefenseAssistant;
            bool ok = optPDefenseLib.Add(myPDefenseLib);
            if (!ok) return ErrorMessage.DBOptError;
            else return ErrorMessage.OK; 
        }
        /// <summary>
        /// 编辑一条答辩记录。若成功返回true。
        /// </summary>
        /// <param name="defenseinfo">答辩记录实体对象</param>
        /// <returns></returns>
        public string EditDefenseInfo(DefenseInfo defenseinfo)
        {
            GMS.ORM.Model.PDefenseLib myPDefenseLib = new GMS.ORM.Model.PDefenseLib();
            GMS.ORM.DAL.PDefenseLib optPDefenseLib = new GMS.ORM.DAL.PDefenseLib();
            myPDefenseLib.def_id = defenseinfo.Id;
            //判断记录是否存在
            bool IfExist = optPDefenseLib.Exists(myPDefenseLib.def_id);
            if (IfExist)
            {
                myPDefenseLib.def_time = defenseinfo.DefenseTime;
                myPDefenseLib.def_room = defenseinfo.DefenseRoom;
                myPDefenseLib.def_tch_1 = defenseinfo.DefenseTeacher1;
                myPDefenseLib.def_tch_2 = defenseinfo.DefenseTeacher2;
                myPDefenseLib.def_tch_3 = defenseinfo.DefenseTeacher3;
                myPDefenseLib.def_tch_4 = defenseinfo.DefenseTeacher4;
                myPDefenseLib.def_tch_5 = defenseinfo.DefenseTeacher5;
                myPDefenseLib.def_assistant = defenseinfo.DefenseAssistant;
                bool ok = optPDefenseLib.Update(myPDefenseLib);
                if (!ok) return ErrorMessage.DBOptError;
                else return ErrorMessage.OK;
            }
            else
                return ErrorMessage.AlreadyExitsData;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string DeleteDefenseInfo(decimal id)
        {
            GMS.ORM.DAL.PDefenseLib optPD = new GMS.ORM.DAL.PDefenseLib();
            if (!optPD.Exists(id)) return ErrorMessage.NoSuchData;
            else
            {
                bool ok = optPD.Delete(id);
                if (!ok) return ErrorMessage.DBOptError;
                return ErrorMessage.OK;
            }
        }
    }
}
